
$(function() {
	
	$("input[id='code_s']:visible").autocomplete(Test.dept);
});

var Dept = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_add")[0];
	
				var body = { };
				body.code = form.code.value;
				body.name = form.name.value;
				body.emp = form.emp.value;
	
				body.tel = form.tel.value;
				body.fax = form.fax.value;
				body.email = form.email.value;
				body.address = form.address.value;
				body.content = form.content.value;
	
				body.orderby = form.orderby.value;
				body.status = form.status.value;
	
				$(this).dialog("close");
	
				$.post('info.php?do=Dept.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},	
	
	popup: function(e, p2, id, dept) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'info.php?do=Dept.edit&id=' + id;	
		var c2 = 'info.php?do=Dept.view&id=' + id;
		var c3 = 'info.php?do=Dept.copy&id=' + id;
		
		var c8 = 'info.php?do=DeptEmp.dept&id=' + id;

		var a1 = ['编辑信息', 'url', 'Dept.dialog_edit_', '600x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x400'];
		var a3 = ['复制信息', 'url', 'Dept.dialog_copy_', '600x400'];
		
		var a8 = ['分配主管', 'url', 'Dept.dialog_emp_', '650x500'];
			
		var s = c1 + ", " + c2 + ", " + c3 + ", " + c8;

		return popup(e, [a1, a2, a3, a8], s);
	},
		
	dialog_edit_: function(url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				body.code = form.code.value;
				body.name = form.name.value;
				body.emp = form.emp.value;

				body.tel = form.tel.value;
				body.fax = form.fax.value;
				body.email = form.email.value;
				body.address = form.address.value;
				body.content = form.content.value;

				body.orderby = form.orderby.value;
				body.status = form.status.value;

				$(this).dialog("close");
				
				var url = 'info.php?do=Dept.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},

	dialog_copy_: function(url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.code = form.code.value;
				body.name = form.name.value;
				body.emp = form.emp.value;

				body.tel = form.tel.value;
				body.fax = form.fax.value;
				body.email = form.email.value;
				body.address = form.address.value;
				body.content = form.content.value;

				body.orderby = form.orderby.value;
				body.status = form.status.value;

				$(this).dialog("close");

				$.post('info.php?do=Dept.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	dialog_emp_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var foo = [];
				
				$("#emp option").each(function(i, n) {
					foo[i] = $(n).val();
				});
				
				var form = $("#form_edit")[0];

				var body = { };
				body.id = form.id.value;
				body.list = foo.join(",");

				$(this).dialog("close");

				var url = 'info.php?do=DeptEmp.deptPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					// $(result).html(data);
					alert("更新成功！");
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'info.php?do=Dept.delete' + p2;
		
		var c4 = 'emp.php?do=User.dept';
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		
		var a4 = ['南山员工', 'url', 'jswin', '1024x768'];
		var a5 = ['沙井员工', 'url', 'jswin', '1024x768'];
		var a6 = ['清溪员工', 'url', 'jswin', '1024x768'];
		var a9 = ['全部员工', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c4 + "&office=1, " + c4 + "&office=2, " + c4 + "&office=3, " + c4;

		return popup(e, [a1, a4, a5, a6, a9], s);
	}
}
